package com.thghh.fsbrowser.fs;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import com.thghh.fsbrowser.annotation.FileSystemType;

/**
 * com.thghh.fsbrowser.fs.LocalFilePathSystem
 * 
 * @author pengzhikang
 * @date 2018年9月24日下午1:06:20
 * @since 1.0
 */
@FileSystemType()
class LocalFileSystem extends CommonFileSystem implements IFileSystem {

	public LocalFileSystem() {
	}

	@Override
	public boolean exists(FilePath filePath) {
		File file = new File(filePath.getPath());
		return file.exists();
	}

	@Override
	public boolean isDirectory(FilePath filePath) {
		File file = new File(filePath.getPath());
		return file.isDirectory();
	}

	@Override
	public boolean isFile(FilePath filePath) {
		File file = new File(filePath.getPath());
		return file.isFile();
	}

	@Override
	public long getLength(FilePath filePath) {
		File file = new File(filePath.getPath());
		return file.length();
	}

	@Override
	public boolean createNewFile(FilePath filePath) throws IOException {
		File file = new File(filePath.getPath());
		return file.createNewFile();
	}

	@Override
	public boolean delete(FilePath filePath) {
		File file = new File(filePath.getPath());
		return file.delete();
	}

	@Override
	public String[] list(FilePath filePath) {
		File file = new File(filePath.getPath());
		return file.list();
	}

	@Override
	public boolean createDirectory(FilePath filePath) {
		File file = new File(filePath.getPath());
		return file.mkdirs();
	}

	@Override
	public boolean rename(FilePath source, FilePath dest) {
		File s = new File(source.getPath());
		File d = new File(dest.getPath());
		return s.renameTo(d);
	}

	@Override
	public int compare(FilePath fp1, FilePath fp2) {
		File f1 = new File(fp1.getPath());
		File f2 = new File(fp2.getPath());
		return f1.compareTo(f2);
	}

	@Override
	public InputStream open(FilePath filePath) throws IOException {
		return new FSInputStream(new FileInputStream(new File(filePath.getPath())), this);
	}

	@Override
	public OutputStream create(FilePath filePath) throws IOException {
		return new FSOutputStream(new FileOutputStream(new File(filePath.getPath())), this) ;
	}

	@Override
	public boolean checkAccess(FilePath f, int access) {
		boolean isAsscess = false;
		File file = new File(f.getPath());
		switch (access) {
		case ACCESS_EXECUTE:
			isAsscess = file.canExecute();
			break;
		case ACCESS_WRITE:
			isAsscess = file.canWrite();
			break;
		case ACCESS_READ:
			isAsscess = file.canRead();
			break;
		}
		return isAsscess;
	}

	@Override
	public boolean setPermission(FilePath f, int access, boolean enable, boolean owneronly) {
		boolean isAsscess = false;
		File file = new File(f.getPath());
		switch (access) {
		case ACCESS_EXECUTE:
			isAsscess = file.setExecutable(enable, owneronly);
			break;
		case ACCESS_WRITE:
			isAsscess = file.setWritable(enable, owneronly);
			break;
		case ACCESS_READ:
			isAsscess = file.setReadable(enable, owneronly);
			break;
		}
		return isAsscess;
	}

	@Override
	public long getLastModifiedTime(FilePath f) {
		File file = new File(f.getPath());
		return file.lastModified();
	}

	@Override
	public boolean setLastModifiedTime(FilePath f, long time) {
		File file = new File(f.getPath());
		return file.setLastModified(time);
	}

	@Override
	public boolean setReadOnly(FilePath f) {
		File file = new File(f.getPath());
		return file.setReadOnly();
	}

	@Override
	public FilePath[] listRoots() {
		File[] files = File.listRoots();
		FilePath[] f = new FilePath[files.length];
		for (int i = 0; i < files.length; i++) {
			f[i] = new FilePath(this, files[i].getPath());
		}
		return f;
	}

	public static final int SPACE_TOTAL = 0;
	public static final int SPACE_FREE = 1;
	public static final int SPACE_USABLE = 2;

	@Override
	public long getSpace(FilePath f, int t) {
		long space = 0;
		File file = new File(f.getPath());
		switch (t) {
		case SPACE_TOTAL:
			space = file.getTotalSpace();
			break;
		case SPACE_FREE:
			space = file.getFreeSpace();
			break;
		case SPACE_USABLE:
			space = file.getUsableSpace();
			break;
		}
		return space;
	}

	@Override
	public int hashCode(FilePath f) {
		File file = new File(f.getPath());
		return file.hashCode();
	}
	
	@Override
	public String tmpdir() {
		return System.getProperty("java.io.tmpdir");
	}

	@Override
	public void close() throws IOException {
		
	}

	@Override
	public boolean isConnected() {
		return true;
	}

}
